### Jonathan D. Klingler, Gary E. Hollibaugh, Jr., and Adam J. Ramey
### "Don't Know What You Got: A Bayesian Hierarchical Model of Neuroticism and Ideological Uncertainty
### Political Science Research and Methods
###
###
### Bootstrapped Aldrich-McKelvey Estimation Code
### Description: This file estimates bootstrapped Aldrich-McKelvey estimates.
###
### Note 1: Run the Preprocessing_and_Summary.R file beforehand to generate the CCES2014.Rda file.
### Note 2: Make sure the directories are set up properly and that all files are in this file's directory.
### Note 3: For ease of use, a template script file called AMBoot.sh is included in the "Cluster Scripts" directory.
### Note 4: This file will start a process that will take several hours to complete, so a cluster might be recommended.


rm(list=ls())
ptm <- proc.time()


# code to generate bootstrapped aldrich-mckelvey estimates
# (intended to be sent to a cluster since it takes a while)

library(basicspace)
load("CCES2014.Rda")

# standard aldrich-mckelvey
# need to remove those where a question wasn't asked (coded as 10)
# otherwise will make paul and cruz look too far right
newcces <- cces[!is.na(cces$self_emoti) & !is.na(cces$self_consc) & !is.na(cces$self_agree) 
                & !is.na(cces$self_extra) & !is.na(cces$self_openn) & !is.na(cces$CC421a) 
                & !is.na(cces$newsint) 
                & !(cces$CC334C == "Not Asked") 
                & !(cces$CC334D == "Not Asked") 
                & !(cces$CC334E == "Not Asked") 
                & !(cces$CC334F == "Not Asked") 
                & !(cces$CC334G == "Not Asked") 
                & !(cces$CC334K == "Not Asked") 
                & !(cces$CC334L == "Not Asked") 
                & !(cces$CC334M == "Not Asked") 
                & !(cces$CC334W == "Not Asked"),]


aldmckspace <- cbind(as.numeric(newcces$CC334A), # self
                     as.numeric(newcces$CC334C), # obama
                     as.numeric(newcces$CC334D), # clinton
                     as.numeric(newcces$CC334E), # cruz
                     as.numeric(newcces$CC334F), # paul
                     as.numeric(newcces$CC334G), # bush
                     as.numeric(newcces$CC334K), # demparty
                     as.numeric(newcces$CC334L), # repparty
                     as.numeric(newcces$CC334M), # teaparty
                     as.numeric(newcces$CC334W)) # scotus


colnames(aldmckspace) <- c("Self", "Obama", "Clinton", "Cruz", "Paul", "Bush", "Democrats", "Republicans", "Tea Party", "Supreme Court")

set.seed(1)
aldmck.ests <- aldmck(aldmckspace, respondent = 1, polarity = 2, missing = c(8, 9, 10))$stimuli
aldmck.ests.rescale <- 2*(aldmck.ests - aldmck.ests[1])/(aldmck.ests[3] - aldmck.ests[1]) - 1
n.boot <- 200000
aldmck.boot <- boot_aldmck(aldmckspace, respondent = 1, polarity = 2, missing = c(8, 9, 10), iter = n.boot) 
save(aldmck.boot, file = "AMBoot.Rda")